import {textContainer} from "@/pixi/textContainer";
import * as PIXI from "pixi.js";
// import {PixiApp} from "@/pixi/App";
import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'

export class Text{
    constructor(_params) {
        let {content,style,name,x,y} = _params

        let user_webset_font = 0;
        if(document.documentElement.currentStyle) {
            user_webset_font = document.documentElement.currentStyle['fontSize'];
        }
        else {
            user_webset_font = getComputedStyle(document.documentElement,false)['fontSize'];
        }
        if(style && style.fontSize){
            style.fontSize = style.fontSize/16 * parseFloat(user_webset_font)
        }else{
            style= {}
            style.fontSize = 16/16 * parseFloat(user_webset_font)
        }

        const text = new PIXI.Text(content, style);
        text.name = name
        // 设置文字位置 100px 100px
        let store = usePixiStore()
        store.changeCurrentWidth()
        store.changeCurrentHeight()

        let {pixiMaxWidth,pixiMaxHeight,currentWidth,currentHeight } = storeToRefs(store)
        text.x = x/pixiMaxWidth.value*currentWidth.value - currentWidth.value / 2;
        text.y = y/pixiMaxHeight.value*currentHeight.value - currentHeight.value / 2;

        // let pixiApp = PixiApp.init()
        // text.x = x - pixiApp.app.screen.width / 2;
        // text.y = y - pixiApp.app.screen.height / 2;
        // 设置文字锚点
        textContainer.addChild(text)
    }

    static create(_params){
        return new Text(_params)
    }
}
